package com.zj.joke;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.zj.joke.R;
import com.zj.joke.adapter.LikeItemAdapter;
import com.zj.joke.db.DBLikeItem;
import com.zj.joke.form.RemoteItem;
import com.zj.joke.util.LikeDbHelper;
import com.zj.joke.util.ShareUtils;

public class LikeActivity extends ZJActivity implements
		OnRefreshListener2<ListView>, OnItemClickListener,
		OnItemLongClickListener, OnClickListener {
	private TextView backTv;
	TextView mTitleTV;
	PullToRefreshListView mListView;
	LikeItemAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.like_show);
		findViews();
		initData();
	}

	private void initData() {
		mListView.setRefreshing();
		// List<DBLikeItem> list = DBLikeItem.getDBLikeItems();
		List<DBLikeItem> list = new LikeDbHelper(mContext).getLikeItems();
		if (list != null && list.size() > 0) {
			mAdapter.setMyLikeItems(list);
		}
		mListView.onRefreshComplete();
	}

	private void findViews() {
		backTv = (TextView) findViewById(R.id.likeBack);
		backTv.setOnClickListener(this);
		mListView = (PullToRefreshListView) findViewById(R.id.likeList);
		mListView.setOnItemClickListener(this);
		mListView.setOnItemLongClickListener(this);
		mListView.setOnRefreshListener(this);
		mListView.setMode(Mode.DISABLED);
		mAdapter = new LikeItemAdapter(this);
		mListView.setAdapter(mAdapter);
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		final Object object = parent.getItemAtPosition(position);
		Dialog alertDialog = new AlertDialog.Builder(this).setTitle("提示")
				.setMessage("确定要删除吗？")
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				})
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (object != null && object instanceof DBLikeItem) {
							DBLikeItem dbLikeItem = (DBLikeItem) object;
							boolean success = new LikeDbHelper(mContext)
									.deleteLikeItem(dbLikeItem);
							if (success) {
								Toast.makeText(LikeActivity.this, "删除成功",
										Toast.LENGTH_SHORT).show();
								mAdapter.deleteDBLikeItem(dbLikeItem);
							} else {
								Toast.makeText(LikeActivity.this, "删除失败",
										Toast.LENGTH_SHORT).show();
							}
							// dbLikeItem.delete();
							// myAdapter.deleteDBLikeItem(dbLikeItem);
						}
					}
				}).create();
		alertDialog.show();
		return true;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Object object = parent.getItemAtPosition(position);
		if (object != null) {
			ShareUtils.showShare(mContext, null, false, object);
		}
	}

	@Override
	public void onBackPressed() {
		finish();
		// overridePendingTransition(R.anim.push_top_in,
		// R.anim.push_bottom_out);
		overridePendingTransition(R.anim.activity_zoom_in,
				R.anim.activity_zoom_out);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.likeBack:
			finish();
			// overridePendingTransition(R.anim.push_top_in,
			// R.anim.push_bottom_out);
			overridePendingTransition(R.anim.activity_zoom_in,
					R.anim.activity_zoom_out);
			break;
		}
	}
}
